/**
 * *******************************************************
 * Copyright © 2013-1014 深圳市美传网络科技有限公司版权所有
 * 创 建 人：gumengfu
 * 创 建 日 期：2014-11-5 上午10:12:27
 * 版 本 号：
 * 修 改 人：
 * 描 述：
 * <p>
 * <p/>
 * </p>
 * *********************************************************
 */
package com.daixu.xutil.demo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

import com.daixu.xutil.demo.widget.systembartint.SystemBarTintManager;

import org.xutils.x;

/**
 * <p>
 * Activity基类<br/>
 * 重载了onCreate，让其创建的时候，自动将Activity添加到AppLoader类的Stack\<Activity\>中<br />
 * 重载了onDestory，让其在摧毁的时候，先把AppLoader下Stack\<Activity\>中对应的移除
 * 写入了公用方法setTitleText(CharSequence);
 * 需要在子activity的layout文件头部添加 android:fitsSystemWindows="true"
 * </p>
 *
 * @author gumengfu
 * @date 2014-11-5
 * @version 1.0
 * @since 1.0
 */
public abstract class NoSwipeBaseActivity extends Activity {
    private AppLoader mAppLoader;
    protected static final int REQUEST_CODE_1 = 0x00000010;
    protected static final int REQUEST_CODE_2 = 0x00000020;
    protected static final int REQUEST_CODE_3 = 0x00000030;
    protected Button sBackBtn;

    private int color = R.color.title_bg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mAppLoader = AppLoader.getInstance();
        mAppLoader.addActivity(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(getResources().getColor(getStatusBarTintColor()));
            } else {
                setTranslucentStatus(true);
            }
        }

        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(getStatusBarTintColor());
    }

    public void setStatusBarTintColor(int color) {
        this.color = color;
    }

    private int getStatusBarTintColor() {
        return color;
    }

    /**
     * 设置通知栏的状态
     *
     * @param on
     */
    @SuppressLint("InlinedApi")
    private void setTranslucentStatus(boolean on) {
        Window win = this.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    protected void onDestroy() {
        // 将本对象从ActivityStack中移除
        mAppLoader.removeActivity(this);
        super.onDestroy();
    }

    /*******************************************************
     * 说明：快速设置标题名，标题组件的的id必须为text_title;
     *
     * @author gumengfu
     * @param text
     ********************************************************/
    protected void setTitleText(CharSequence text) {
        TextView textView = (TextView) findViewById(R.id.tv_title);
        if (textView != null) {
            textView.setText(text);
        }
    }

    /*******************************************************
     * 说明：快速设置标题名，标题组件的的id必须为text_title;
     *
     * @author gumengfu
     * @param resId
     ********************************************************/
    protected void setTitleText(int resId) {
        TextView textView = (TextView) findViewById(R.id.tv_title);
        if (textView != null) {
            textView.setText(resId);
        }
    }

    protected void showBackButton() {
        sBackBtn = (Button) findViewById(R.id.btn_back);
        if (sBackBtn != null)
            sBackBtn.setVisibility(View.VISIBLE);
    }

    public void doBack(View v) {
        finish();
    }

    @Override
    public void finish() {
        // 如果存在软键盘，关闭软键盘
        hideInputMethod();
        super.finish();
    }

    protected void hideInputMethod() {
        InputMethodManager imm = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
        if (getCurrentFocus() != null && imm.isActive()) {
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /*******************************************************
     * 说明：显示网络错误的对话框
     *
     * @author yangbo
     *******************************************************/
    protected void showNetworkErrorDialog() {
//		CustomDialog dialog = new CustomDialog.Builder(AppLoader.getInstance().currentActivity()).setTitle(R.string.network_error_title)
//				.setMessage(R.string.network_error_msg).setPositiveButton(R.string.setting_network, new DialogInterface.OnClickListener() {
//
//					@Override
//					public void onClick(DialogInterface dialog, int position) {
//						dialog.dismiss();
//						Intent wifiSetting = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
//						startActivity(wifiSetting);
//					}
//				}).setNegativeButton(R.string.cancel, null).create();
//		dialog.show();
    }
}
